import {TableConstants} from 'ebom/constant/Constants.js'
import {ArrayUtils} from "../utils/ArrayUtils";


/**
 * 图纸相关工具类
 * @type {{}}
 */
export const DrawingUtils = {

    /**
     * 创建图纸
     * @param partMasterId
     * @param partIterationId
     * @returns {{masterId: string, iterationId: string}}
     */
    createDrawing: function (partMasterId, partIterationId) {
        // 1.创建主对象
        const masterId = this.getDrawingMasterId(partMasterId);

        // 2.创建图纸版次对象
        const iterationId = informat.table.insert(TableConstants.drawingIteration, {
            'partIterationRef': partIterationId,
            'drawingMasterId': masterId,
        });

        // 3. 更新记录
        informat.table.update(TableConstants.drawingMaster, {
            'id': masterId,
            'drawingIterationId': iterationId,
        });

        // 4. 添加关联列表
        informat.table.addRelation(TableConstants.drawingMaster, 'drawingIterationList', masterId, iterationId);

        return {
            masterId: masterId,
            iterationId: iterationId,
        }
    },

    getDrawingMasterId: function (partMasterId) {
        let masterId = '';
        let result = informat.table.queryList(TableConstants.drawingMaster, {
            filter: {
                opt: 'and',
                conditionList: [
                    {fieldId: 'partMasterRef', opt: 'eq', value: partMasterId},
                ],
            }
        });
        if (ArrayUtils.isEmpty(result)) {
            masterId = informat.table.insert(TableConstants.drawingMaster, {
                'partMasterRef': partMasterId,
            })
        } else {
            masterId = result[0].id;
        }
        return masterId;
    }


};